<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\Link as LinkModel;
class Link extends Common
{
	public function lst()
    { 
        $link=new LinkModel();
        if(request()->isPost()){
            $data=input('post.');

            foreach ($data as $k => $v) {
                $link->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新成功！',url('link/lst'));
            return;
        }
       $linkres=$link->order('sort desc')->paginate(3);
        $this->assign('linkres',$linkres);
        return view('list');
    }

    public function add()
    {
        if(request()->isPost()){
             $data=input('post.');

            $validate = \think\Loader::validate('Link');//实例化link验证器
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
           
            $insert=LinkModel::create($data);
            if($insert){
                $this->success('添加友情链接成功！',url('link/lst'));
            }else{
                $this->error('添加友情链接失败！');
            }
            return;
        }
        return view();
    }

    public function edit()
    {
        $link=new LinkModel();
        if(request()->isPost()){
              $data=input('post.');

              $validate = \think\Loader::validate('Link');//实例化link验证器
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }

             $save=$link->update($data,['id' => $data['id']]);
            if($save){
                $this->success('修改友情链接成功！',url('link/lst'));
            }else{
                $this->error('修改友情链接失败！');
            }
            return;
        }
        $links=$link->find(input('id'));
        //dump($links);die;
        $this->assign('links',$links);
        return view();
    }

    public function delete()
    {
        $del=db('link')->delete(input('id'));
        if($del){
             $this->success('删除友情链接成功！',url('link/lst'));
            }else{
                $this->error('删除友情链接失败！');
            }
        return view();
    }
}
